import { reactive } from 'vue'

// 创建一个响应式的状态存储
const store = reactive({
  // 用户信息
  user: JSON.parse(localStorage.getItem('user')) || null,
  token: localStorage.getItem('token') || null,
  
  // 设置用户信息和token
  setUserInfo(user, token) {
    this.user = user
    this.token = token
    localStorage.setItem('user', JSON.stringify(user))
    localStorage.setItem('token', token)
  },
  
  // 清除用户信息
  clearUserInfo() {
    this.user = null
    this.token = null
    localStorage.removeItem('user')
    localStorage.removeItem('token')
  },
  
  // 检查是否已登录
  isLoggedIn() {
    return !!this.token
  },
  
  // 检查是否是管理员
  isAdmin() {
    return this.user && this.user.role === 'admin'
  }
})

export default store